package com.example.testhotload;

import android.app.DownloadManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.preference.PreferenceManager;
import android.util.Log;

public class DownloadApk {
    public static final  String TAG = DownloadApk.class.getSimpleName();
    public static void download(Context context ,String url,String title,final String apkName){
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        long downloadId = sp.getLong(DownloadManager.EXTRA_DOWNLOAD_ID,-1L);
        if(downloadId!=-1L){
            FileDownloadManager fdm = FileDownloadManager.getInstance(context);
            int status = fdm.getDownloadStatus(downloadId);
            if(status==DownloadManager.STATUS_SUCCESSFUL){
                Uri uri = fdm.getDownloadUri(downloadId);
                if(uri!=null){
                    if(compare(getApkInfo(context,uri.getPath()),context)){
                        startInstall(context,uri);
                        return ;
                    }else{
                        fdm.getDownloadManager().remove(downloadId);
                    }
                }
                start(context,url,title,apkName);
            }else if(status==DownloadManager.STATUS_FAILED){
                start(context,url,title,apkName);
            }else{
                Log.d(TAG,"apk is already downloading");
            }
        }else{
            start(context,url,title,apkName);
        }
    }

    private  static void start(Context context,String url,String title,String apkName){
        long id = FileDownloadManager.getInstance(context).StartDownload(url,title,"下载后点击打开",apkName);
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        sp.edit().putLong(DownloadManager.EXTRA_DOWNLOAD_ID,id).commit();
        Log.d(TAG,"apk start download"+id);
    }

    public static void startInstall(Context context,Uri uri){
        Intent install = new Intent(Intent.ACTION_VIEW);
        install.setDataAndType(uri,"application/vnd.android,package-archive");
        install.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        context.startActivity(install);
    }

    private static PackageInfo getApkInfo(Context context,String path){
        PackageManager pm  =context.getPackageManager();
        PackageInfo info = pm.getPackageArchiveInfo(path,PackageManager.GET_ACTIVITIES);
        if(info!=null)return info;
        return null;

    }

    private static boolean compare(PackageInfo apkInfo,Context context){
        if(apkInfo ==null)return false;
        String localPackage = context.getPackageName();
        if(apkInfo.packageName.equals(localPackage)){
            try{
                PackageInfo packageInfo = context.getPackageManager().getPackageInfo(localPackage,0);
                if(apkInfo.versionCode>packageInfo.versionCode){
                    return true;
                }
            }catch (PackageManager.NameNotFoundException e){
                e.printStackTrace();
            }
        }
        return false;
    }
}
